home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
datecls.exe
/
DATECLS4.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-07
|
12KB
|
626 lines
/*
*┌──────────────────────────────────────────────────────────────────────
*│ File.........: DATECLS4.CPP
*│ Date.........: Sunday 3/7/1993
*│ Author.......: Kenneth A. Argo (CIS 71241,3635)
*│ Copyright....: None! Use freely.
*│ Version......: 4.1 Compile w/MSC++ 7.0
*│ Usage........: General purpose date conversion, arithmetic,
*│ : comparison, and formatting class
*│
*│ See DATE.H for acknowledgements and compile/link notes.
*└──────────────────────────────────────────────────────────────────────
*/
#include "datecls4.h"
int Date::DisplayFormat=MDY;
unsigned char Date::DisplayOptions='\0';
const char *dayname[] = {"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"} ;
const char *mname[] = {"January","February","March","April","May",
"June","July","August","September","October","November","December"};
static int GauDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
////////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////////
Date::Date()
{
month = day = day_of_week = 0;
year = 0;
julian = 0;
}
Date::Date (const long j) : julian(j)
{
julian_to_mdy ();
}
Date::Date (const int m, const int d, const int y) : month((unsigned char)m), day((unsigned char)d), year(y)
{
mdy_to_julian ();
}
Date::Date (char *dat)
{
if (!_stricmp(dat, "TODAY"))
{
struct _dosdate_t temp_date;
_dos_getdate(&temp_date);
month = temp_date.month;
day = temp_date.day;
year = temp_date.year;
}
else
{
month = (unsigned char)atoi(strtok(dat,"/-"));
day = (unsigned char)atoi(strtok(NULL,"/-"));
year = atoi(strtok(NULL," "));
}
mdy_to_julian ();
}
Date::Date (const _dosdate_t &ds)
{
month = ds.month;
day = ds.day;
year = ds.year;
mdy_to_julian ();
}
Date::Date (const Date &dt)
{
month = dt.month;
day = dt.day;
year = dt.year;
mdy_to_julian ();
}
Date::~Date()
{
if (buf != (char *)&BadDate)
delete buf;
}
//////////////////////////////////////////////////////////////
// Conversion operations
//////////////////////////////////////////////////////////////
Date::operator char *( void )
{
buf = new char[13];
if (day==0 || month==0 || year==0)
buf = (char *)&BadDate;
else
sprintf(buf,"%1d/%1d/%-4d",month,day,year);
return buf;
}
//////////////////////////////////////////////////////////////
// Date Arithmetic
//////////////////////////////////////////////////////////////
Date &Date::operator + (const long i)
{
return Date(julian + i);
}
Date &Date::operator + (const int i)
{
return Date(julian + (long)i);
}
Date &Date::operator - (const long i)
{
return Date(julian - i);
}
Date &Date::operator - (const int i)
{
return Date(julian - (long)i);
}
long Date::operator - (const Date &dt)
{
return ( julian - dt.julian );
}
Date &Date::operator += (const long i)
{
julian += i;
julian_to_mdy();
return *this;
}
Date &Date::operator -= (const long i)
{
julian -= i;
julian_to_mdy();
return *this;
}
Date &Date::operator ++()
{
julian++;
julian_to_mdy();
return *this;
}
Date &Date::operator ++(int)
{
julian++;
julian_to_mdy();
return *this;
}
Date &Date::operator --()
{
julian--;
julian_to_mdy();
return *this;
}
Date &Date::operator --(int)
{
julian--;
julian_to_mdy();
return *this;
}
//////////////////////////////////////////////////////////////
// Date comparison
//////////////////////////////////////////////////////////////
int operator < (const Date &dt1, const Date &dt2)
{
return ( dt1.julian < dt2.julian );
}
int operator <= (const Date &dt1, const Date &dt2)
{
return ( (dt1.julian == dt2.julian) || (dt1.julian < dt2.julian) );
}
int operator > (const Date &dt1, const Date &dt2)
{
return ( dt1.julian > dt2.julian );
}
int operator >= (const Date &dt1, const Date &dt2)
{
return ( (dt1.julian == dt2.julian) || (dt1.julian > dt2.julian) );
}
int operator == (const Date &dt1, const Date &dt2)
{
return ( dt1.julian == dt2.julian );
}
int operator != (const Date &dt1, const Date &dt2)
{
return ( dt1.julian != dt2.julian );
}
////////////////////////////////////////////////////////////////
// Ostream operations
////////////////////////////////////////////////////////////////
ostream &operator << (ostream &os, const Date &dt)
{
return os << dt.formatDate();
}
ostream &operator << (ostream &os, const _dosdate_t &dt)
{
return os << (int)dt.month << "/" << (int)dt.day << "/" << dt.year;
}
//////////////////////////////////////////////////////////////
// Conversion routines
//////////////////////////////////////////////////////////////
void Date::julian_to_wday (void)
{
day_of_week = (unsigned char) ((julian + 2) % 7 + 1);
}
void Date::julian_to_mdy ()
{
long a,b,c,d,e,z,alpha;
z = julian+1;
// dealing with Gregorian calendar reform
if (z < 2299161L)
a = z;
else
{
alpha = (long) ((z-1867216.25) / 36524.25);
a = z + 1 + alpha - alpha/4;
}
b = ( a > 1721423 ? a + 1524 : a + 1158 );
c = (long) ((b - 122.1) / 365.25);
d = (long) (365.25 * c);
e = (long) ((b - d) / 30.6001);
day = (unsigned char)(b - d - (long)(30.6001 * e));
month = (unsigned char)((e < 13.5) ? e - 1 : e - 13);
year = (int)((month > 2.5 ) ? (c - 4716) : c - 4715);
julian_to_wday ();
}
void Date::mdy_to_julian (void)
{
int a,b=0;
int work_month=month, work_day=day, work_year=year;
// correct for negative year
if (work_year < 0)
work_year++;
if (work_month <= 2)
{
work_year--;
work_month +=12;
}
// deal with Gregorian calendar
if (work_year*10000. + work_month*100. + work_day >= 15821015.)
{
a = (int)(work_year/100.);
b = 2 - a + a/4;
}
julian = (long) (365.25*work_year) +
(long) (30.6001 * (work_month+1)) + work_day + 1720994L + b;
julian_to_wday ();
}
////////////////////////////////////////////////////////////////
// Format routine
////////////////////////////////////////////////////////////////
char *Date::formatDate (const int type) const
{
static char buf[40];
_strnset( buf, '\0', sizeof(buf) );
switch ( type )
{
case DAY:
if ( (day_of_week < 1) || (day_of_week > 7) )
strcpy(buf,"invalid day");
else
strncpy( buf, dayname[day_of_week-1],
(DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
return buf;
break;
case MONTH:
if ( (month < 1) || (month > 12) )
strcpy(buf,"invalid month");
else
strncpy( buf, mname[month-1],
(DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
return buf;
break;
case FULL:
if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
(day_of_week > 7) )
{
strcpy(buf,"invalid date");
return buf;
}
strncpy( buf, dayname[day_of_week-1],
(DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
strcat( buf, ", ");
strncat( buf, mname[month-1],
(DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
strcat( buf, " ");
sprintf( buf+strlen(buf), "%d, %d", day, abs(year) );
if (year < 0)
strcat(buf," B.C.E.");
return buf;
break;
case EUROPEAN:
if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
(day_of_week > 7) )
{
strcpy(buf,"invalid date");
return buf;
}
sprintf(buf,"%d ", day);
strncat(buf, mname[month-1],
(DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
sprintf( buf+strlen(buf), " %d", abs(year) );
if (year < 0)
strcat(buf," B.C.E.");
return buf;
break;
case MDY:
default:
if (day==0 || month==0 || year==0)
strcpy(buf,"invalid date");
else
sprintf( buf+strlen(buf), "%1d/%1d/%02d", month, day,
(DisplayOptions & NO_CENTURY) && (abs(year) > 1899)
? (abs(year) - (abs(year) / 100 * 100))
: (abs(year)) );
return buf;
break;
}
}
void Date::setFormat( const int format )
{
DisplayFormat = format;
}
int Date::